home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13113 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  61 lines

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can you printf a long
  5. Date: 4 Apr 1996 10:24 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <4APR199610244914@erich.triumf.ca>
  9. References: <4ju8o1$dc3@news.netam.net>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4ju8o1$dc3@news.netam.net>, bgc@alpha.netam.net (The Bowling Green Connection) writes...
  14. >I had some trouble with this code:
  15. >(I'm using gcc on Digital Unix)
  16. >int main() {
  17. >    long i;
  18. >    while(1) {
  19. >    i++;
  20. >    printf("%f\n", i);
  21. >    }
  22. >    return 0;
  23. >}
  24. >The numbers printed to the screen as 0.00000.
  25.  
  26. %f is used to print floats - what happens when you pass it an integer type (int
  27. or long) is undefined, but highly unlikely to produce any  meaningful result.
  28.  
  29. >But when I changed %f to %d, the numbers printed correctly.
  30. >I thought that %d had the same limitations as an int..that is,
  31. >it could only print about 4 bits of information, whereas a long
  32. >is capable of more (8 or 16 bits), so wouldn't %f (float) be able
  33. >to better handle those long numbers?  And why did %d work
  34. >correctly, even up to 634,000 (that's when my disk space ran out.. :))
  35.  
  36. You mean "bytes", not "bits".
  37.  
  38. An int must be _at_ _least_ 2 bytes, but may be longer.  A long must be  at
  39. least as long as an int, but may (or may not) be longer.  It seems that on your
  40. system, both int and long are 32 bits (4 bytes).
  41.  
  42. (Under MS-DOS, an int is (for most compilers) 16 bits, and a long is 32 bits.)
  43.  
  44. Floats are stored in a quite different format than ints and longs.
  45.  
  46. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  47. Internet: bennett@triumf.ca         | of one another only when one can be
  48. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  49. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  50. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  51. or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.